package com.tstudy.laoshibang.active;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.login.LoginActivity;
import com.tstudy.laoshibang.mode.Active;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
/**
 *  活动列表adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class ActiveListAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<Active> mItems;
	int lineHeight = CommonUtil.dip2px(1);
	
	String viewCountStr = BaseApplication.getResString(R.string.active_list_view_count);
	String freeStr = BaseApplication.getResString(R.string.active_list_free);
	int redTextColor = BaseApplication.getResColor(R.color.theme_red);
	int grayTextColor = BaseApplication.getResColor(R.color.text_color_gray);
	public ActiveListAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<Active> items) {
		setData(items,true);
	}
	public void setData(List<Active> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<Active> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.active_list_item, null);
			viewHolder.pic = (ImageView)convertView.findViewById(R.id.active_list_item_pic);
			viewHolder.title = (TextView) convertView.findViewById(R.id.active_list_item_title);
			viewHolder.content = (TextView) convertView.findViewById(R.id.active_list_item_content);
			viewHolder.price = (TextView) convertView.findViewById(R.id.active_list_item_price);
			viewHolder.viewCount = (TextView) convertView.findViewById(R.id.active_list_item_view_count);
			viewHolder.loveCount = (TextView) convertView.findViewById(R.id.active_list_item_love_count);
			viewHolder.loveIcon = (ImageView) convertView.findViewById(R.id.active_list_item_love_icon);
			viewHolder.sp = (View) convertView.findViewById(R.id.active_list_item_sp);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
//		convertView.setOnClickListener(null);
		return convertView;
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final Active active = (Active) getItem(position);
		viewHolder.title.setText(active.title);
		viewHolder.content.setText(active.summary);
		if (active.isCharge == CONSTANT.IS_CHARGE) {
			viewHolder.price.setText(active.price+"");
		}else{
			viewHolder.price.setText(freeStr);
		}
		RelativeLayout.LayoutParams spLp = (LayoutParams) viewHolder.sp.getLayoutParams();
		if (position == mItems.size()-1) {
			spLp.height = 1;
		}else{
			spLp.height = lineHeight;
		}
		viewHolder.viewCount.setText(String.format(viewCountStr, active.accessNum));
		if (active.isPraise == CONSTANT.IS_PRISED) {
			viewHolder.loveCount.setTextColor(redTextColor);
		}else{
			viewHolder.loveCount.setTextColor(grayTextColor);
		}
		viewHolder.loveCount.setText(active.praiseNum + "");
		viewHolder.loveIcon.setSelected(active.isPraise == 1);
		HttpManager.getInstance().loadCommonImage(viewHolder.pic,active.imgIdxName);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onAvatorClick(active.actId,position);
				}
			}
		});
		viewHolder.loveIcon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (BaseApplication.checkLogin()) {
					if (mActionCallBack != null) {
						active.isPraise = active.isPraise==1?0:1;
						v.setSelected(active.isPraise == 1);
						active.praiseNum = active.isPraise == 1?active.praiseNum+1:active.praiseNum-1;
						if (active.isPraise == CONSTANT.IS_PRISED) {
							viewHolder.loveCount.setTextColor(redTextColor);
						}else{
							viewHolder.loveCount.setTextColor(grayTextColor);
						}
						viewHolder.loveCount.setText(active.praiseNum+"");
						CommonUtil.loveAnimator(v);
						mActionCallBack.onLoveClick(active, position,viewHolder.loveCount);
					}
				}else{
					LoginActivity.show();
				}
			}
		});
	}
	
	static class ViewHolder{
		ImageView pic;
		TextView title;
		TextView content;
		TextView price;
		TextView viewCount;
		TextView loveCount;
		ImageView loveIcon;
		View sp;
	}
	
	public interface AdapterCallBack{
		public void onAvatorClick(int activeId,int position);
		public void onLoveClick(Active active,int position,TextView view);
	}
	AdapterCallBack mActionCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
